package com.converter.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Collections;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Auto extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Auto frame = new Auto();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Auto() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}
	
	public static  void autosujession (){
		
		
		
		
		
		
	}

	class ComboKeyHandler extends KeyAdapter{
		  private final JComboBox comboBox;
		  private final Vector list = new Vector();
		  public ComboKeyHandler(JComboBox combo) {
		    this.comboBox = combo;
		    for(int i=0;i< comboBox.getModel().getSize();i++) {
		      list.addElement((String)comboBox.getItemAt(i));
		    }
		  }
		  private boolean shouldHide = false;
		  @Override public void keyTyped(final KeyEvent e) {
		    EventQueue.invokeLater(new Runnable() {
		      @Override public void run() {
		        String text = ((JTextField)e.getSource()).getText();
		        if(text.length()==0) {
		          setSuggestionModel(comboBox, new DefaultComboBoxModel(list), "");
		          comboBox.hidePopup();
		        }else{
		          ComboBoxModel m = getSuggestedModel(list, text);
		          if(m.getSize()==0 || shouldHide) {
		            comboBox.hidePopup();
		          }else{
		            setSuggestionModel(comboBox, m, text);
		            comboBox.showPopup();
		          }
		        }
		      }
		    });
		  }
		  @Override public void keyPressed(KeyEvent e) {
		    JTextField textField = (JTextField)e.getSource();
		    String text = textField.getText();
		    shouldHide = false;
		    switch(e.getKeyCode()) {
		      case KeyEvent.VK_RIGHT:
		        for(String s: list) {
		          if(s.startsWith(text)) {
		            textField.setText(s);
		            return;
		          }
		        }
		        break;
		      case KeyEvent.VK_ENTER:
		        if(!list.contains(text)) {
		          list.addElement(text);
		          Collections.sort(list);
		          setSuggestionModel(comboBox, getSuggestedModel(list, text), text);
		        }
		        shouldHide = true;
		        break;
		      case KeyEvent.VK_ESCAPE:
		        shouldHide = true;
		        break;
		    }
		  }
		  private static void setSuggestionModel(
		      JComboBox comboBox, ComboBoxModel mdl, String str) {
		    comboBox.setModel(mdl);
		    comboBox.setSelectedIndex(-1);
		    ((JTextField)comboBox.getEditor().getEditorComponent()).setText(str);
		  }
		  private static ComboBoxModel getSuggestedModel(Vector list, String text) {
		    DefaultComboBoxModel m = new DefaultComboBoxModel();
		    for(String s: list) {
		      if(s.startsWith(text)) m.addElement(s);
		    }
		    return m;
		  }
		}
	
}
